package com.yytek.am.config;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.yytek.am.constants.MessageType;
import com.yytek.am.core.rule.EventRule;
import com.yytek.am.core.rule.EventRuleClear;
import com.yytek.am.core.rule.EventRuleGenerate;
import com.yytek.am.core.rule.EventRuleWrapper;
import com.yytek.am.core.runtime.EngineRuntime;
import com.yytek.am.domain.FmDef;
import com.yytek.am.domain.FmDefRlobject;
import com.yytek.am.domain.FmRuleClear;
import com.yytek.am.domain.FmRuleGenerate;
import com.yytek.am.service.IFmDefRlobjectService;
import com.yytek.am.service.IFmDefService;
import com.yytek.am.service.IFmRuleClearService;
import com.yytek.am.service.IFmRuleGenerateService;
import com.yytek.common.client.base.MqConfig;
import com.yytek.common.client.config.MqttConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 初始化告警规则，监听告警源数据，初始化告警引擎
 */
@Configuration
public class EngineRuntimeConfig {

	@Bean
	public EngineRuntime engineRuntime(MqttConfig mqConfig, IFmDefService fmDefService, IFmDefRlobjectService fmDefRlobjectService,
									   IFmRuleGenerateService iFmRuleGenerateService, IFmRuleClearService iFmRuleClearService) {

		// 初始化告警生成，清除规则
		List<EventRuleWrapper> eventRuleWrappers = new ArrayList<EventRuleWrapper>();
		//查询告警定义信息
		List<FmDef> fmDefList  = fmDefService.selectFmDefList(null);
		Map<Long, FmDef> fmDefMap = new HashMap<>();
		if(CollectionUtil.isNotEmpty(fmDefList)){
			fmDefMap = fmDefList.stream()
					.collect(Collectors.toMap(item -> item.getId(), item -> item));
		}
		//查询关联定义信息
		List<FmDefRlobject> fmDefRlobjectList = fmDefRlobjectService.selectFmDefRlobjectList(null);
		Map<Long, List<FmDefRlobject>> fmDefRlobjectMap = new HashMap<>();
		if(CollectionUtil.isNotEmpty(fmDefRlobjectList)){
			fmDefRlobjectMap = fmDefRlobjectList.stream().collect(Collectors.groupingBy(item -> item.getAlarmDefineId()));
		}
		//查询告警生成规则
		List<FmRuleGenerate> fmRuleGenerateList = iFmRuleGenerateService.selectFmRuleGenerateList(null);
		Map<Long, FmRuleGenerate> fmRuleGenerateMap = new HashMap<>();
		if (CollectionUtil.isNotEmpty(fmRuleGenerateList)){
			fmRuleGenerateMap = fmRuleGenerateList.stream()
					.collect(Collectors.toMap(item -> item.getAlarmDefineId(), item -> item));
		}
		//查询告警清除规则
		List<FmRuleClear> fmRuleClearList = iFmRuleClearService.selectFmRuleClearList(null);
		Map<Long, FmRuleClear> fmRuleClearMap = new HashMap<>();
		if (CollectionUtil.isNotEmpty(fmRuleClearList)){
			fmRuleClearMap= fmRuleClearList.stream()
					.collect(Collectors.toMap(item -> item.getAlarmDefineId(), item -> item));
		}

		//根据告警定义标识，将告警生成，清除规则存入队应的对象
		for (Long alarmDefineId : fmDefMap.keySet()) {
			EventRuleWrapper eventRuleWrapper = new EventRuleWrapper();
			Set<String> rlObjects = new HashSet<>();
			if (fmDefRlobjectMap.containsKey(alarmDefineId)) {
				for (FmDefRlobject fmDefRlobject : fmDefRlobjectMap.get(alarmDefineId)
					 ) {
					rlObjects.add(fmDefRlobject.getRlObjectId());
				}
			}
			eventRuleWrapper.setEventRule(createEventRule(fmDefMap.get(alarmDefineId), rlObjects));
			if (fmRuleGenerateMap.containsKey(alarmDefineId)) {
				eventRuleWrapper.setEventRuleGenerate(createEventRuleGenerate(fmRuleGenerateMap.get(alarmDefineId)));
			}
			if (fmRuleClearMap.containsKey(alarmDefineId)) {
				eventRuleWrapper.setEventRuleClear(createEventRuleClear(fmRuleClearMap.get(alarmDefineId)));
			}
			eventRuleWrappers.add(eventRuleWrapper);
		}
		// mq配置
		MqConfig config = new MqConfig();
		BeanUtil.copyProperties(mqConfig, config);
		// 告警源数据
		List<String> topicList = Arrays.asList(MessageType.AM_ALARM_SOURCE);
		// 初始化告警引擎
		EngineRuntime engineRuntime = new EngineRuntime();
		engineRuntime.init(eventRuleWrappers, config, topicList);
		return engineRuntime;
	}

	/**
	 * 创建告警规则定义
	 * @param fmDef
	 * @param rlObjects
	 * @return
	 */
	private EventRule createEventRule(FmDef fmDef, Set<String> rlObjects) {
		EventRule eventRule = new EventRule();
		BeanUtil.copyProperties(fmDef, eventRule);
		eventRule.setRlObjects(rlObjects);
		return eventRule;
	}

	/**
	 * 创建告警生成规则定义
	 * @param fmRuleGenerate
	 * @return
	 */
	private EventRuleGenerate createEventRuleGenerate(FmRuleGenerate fmRuleGenerate) {
		EventRuleGenerate eventRuleGenerate = new EventRuleGenerate();
		BeanUtil.copyProperties(fmRuleGenerate, eventRuleGenerate);
		return eventRuleGenerate;
	}

	/**
	 * 创建告警清除规则定义
	 * @param fmRuleClear
	 * @return
	 */
	private EventRuleClear createEventRuleClear(FmRuleClear fmRuleClear) {
		EventRuleClear eventRuleClear = new EventRuleClear();
		BeanUtil.copyProperties(fmRuleClear, eventRuleClear);
		return eventRuleClear;
	}

}
